// import Mock from 'mockjs'
// const mockModules: any[] = [];

const methodType: Record<string, string> = {
  GET: 'read',
  POST: 'create',
  PUT: 'update',
  DELETE: 'delete'
}

interface Config {
  method: string;
  url: string;
}

const modules = import.meta.globEager('./**/*.js')
const mockModules: Record<string, any> = {}
Object.keys(modules).forEach((key) => {
  mockModules['/' + key.replace('./mocks/', '')] = modules[key].default
})
export const getMockModule = (config: Config) => {
  const url = config.url.split('?')[0] + (methodType[config.method] ? ('/' + methodType[config.method]) : '') + '.js'
  return mockModules[url] ??
  mockModules[config.url.split('?')[0] + '.js']
}
const getMockBody = (config: Config) => {
  const mock = getMockModule(config)()
  mock.data = mock.body
  delete mock.body
  return {
    data: mock.data.data,
    code: mock.data.code,
    success: mock.data.success,
    msg: mock.data.msg
  }
}
export default getMockBody
